package com.zhanggm.springcloud.jpa.controller;

import com.zhanggm.springcloud.jpa.dao.UserRepository;
import com.zhanggm.springcloud.jpa.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/jpa/")
public class JpaController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("getPageInfo")
    public Page<UserEntity> getPageInfo(UserEntity userEntity,
                                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "size",defaultValue = "2") Integer size){
        //封装查询条件
        UserEntity queryUserEntity = new  UserEntity();
        if(userEntity.getUsername()!=null){
            queryUserEntity.setUsername(userEntity.getUsername());
        }
        Example example = Example.of(queryUserEntity);
        //设置分页
        Pageable pageable = PageRequest.of(page-1,size);
        //调用jpa的方法进行查询
        return userRepository.findAll(example,pageable);
    }

    @GetMapping("getPage")
    public Page<UserEntity> getPage(UserEntity userEntity,
                                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "size",defaultValue = "2") Integer size){
        Specification<UserEntity> specification = new Specification<UserEntity>() {
            @Override
            public Predicate toPredicate(Root<UserEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                //存放查询条件
                List<Predicate> predicateList = new ArrayList<>();
                if(userEntity.getUsername()!=null){
                    Predicate usernmaePredicate = cb.like(root.get("username"), userEntity.getUsername());
                    predicateList.add(usernmaePredicate);
                }

                if(userEntity.getIdStart()!=null){
                    Predicate predicate = cb.greaterThanOrEqualTo(root.get("id"), userEntity.getIdStart());
                    predicateList.add(predicate);
                }

                if(userEntity.getIdEnd()!=null){
                    Predicate predicate = cb.lessThanOrEqualTo(root.get("id"), userEntity.getIdEnd());
                    predicateList.add(predicate);
                }

                //返回查询条件
//                Predicate[] predicates = new Predicate[predicateList.size()];
//                predicateList.toArray(predicates);
//                return cb.and(predicates);
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Pageable pageable = PageRequest.of(page-1,size, Sort.Direction.DESC,"username");
        Page<UserEntity> all = userRepository.findAll(specification, pageable);
        return all;
    }
}
